package com.hao.shirojwt.exception;

import com.hao.shirojwt.util.response.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpStatus;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * 异常处理器
 */
@Slf4j
@RestControllerAdvice
public class BDExceptionHandler {

    @ExceptionHandler(BDException.class)
    @ResponseBody
    public BaseResponse handleBDException(BDException e) {
        log.error(e.getMessage(), e);
        return BaseResponse.error(e.getMessage());
    }

    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseBody
    public BaseResponse resolveIllegalArgumentException(IllegalArgumentException e) {
        log.error(e.getLocalizedMessage(), e);
        return new BaseResponse(HttpStatus.UNAUTHORIZED.value() + "", e.getLocalizedMessage());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public BaseResponse handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        log.error(e.getLocalizedMessage(), e);
        FieldError fieldError = e.getBindingResult().getFieldError();
        return new BaseResponse(HttpStatus.UNAUTHORIZED.value() + "", fieldError.getDefaultMessage());
    }

    @ExceptionHandler(DuplicateKeyException.class)
    @ResponseBody
    public BaseResponse handleDuplicateKeyException(DuplicateKeyException e) {
        log.error(e.getMessage(), e);
        return BaseResponse.error("数据库中已存在该记录");
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseBody
    public BaseResponse noHandlerFoundException(NoHandlerFoundException e) {
        log.error(e.getMessage(), e);
        return new BaseResponse(HttpStatus.NOT_FOUND.value() + "", "没有找到相应接口");
    }

    @ExceptionHandler(AuthorizationException.class)
    @ResponseBody
    public BaseResponse handleAuthorizationException(AuthorizationException e) {
        log.error(e.getMessage(), e);
        return new BaseResponse(HttpStatus.FORBIDDEN.value() + "", "未授权");
    }

    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    @ResponseBody
    public BaseResponse handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException e) {
        log.error(e.getMessage(), e);
        return BaseResponse.error(e.getMessage());
    }


    @ExceptionHandler({Exception.class})
    @ResponseBody
    public BaseResponse handleException(Exception e) {
        log.error(e.getMessage(), e);
        return BaseResponse.error("服务器错误，请联系管理员，" + e.getMessage());
    }

}
